/**
 * 文件类型（1 普通文件 2 压缩文件 3 excel 4 word 5 pdf 6 txt 7 图片 8 音频 9 视频 10 ppt 11 源码文件 12 csv）
 */
const getFileFontElement = (filename, type) => {
  let tagStr
  const fileSuffix = filename.substring(filename.lastIndexOf('.'))
  switch (type) {
    case 0:
      tagStr = 'file-type-folder'
      break
    case 1:
      tagStr = fileType(fileSuffix)
      break
    case 2:
      tagStr = 'file-type-archive'
      break
    case 3:
      tagStr = 'file-type-xls'
      break
    case 4:
      tagStr = 'file-type-doc'
      break
    case 5:
      tagStr = 'file-type-pdf'
      break
    case 6:
      tagStr = txtType(fileSuffix)
      break
    case 7:
      tagStr = 'file-type-image'
      break
    case 8:
      tagStr = 'file-type-audio'
      break
    case 9:
      tagStr = 'file-type-video'
      break
    case 10:
      tagStr = 'file-type-ppt'
      break
    case 11:
      tagStr = codeType(fileSuffix)
      break
    default:
      tagStr = 'file-type-file'
      break
  }
  return tagStr
}

const codeType = fileSuffix => {
  let type = ''
  switch (fileSuffix) {
    case '.java':
      type = 'file-type-java'
      break
    case '.vue':
      type = 'file-type-vue'
      break
    case '.yaml':
      type = 'file-type-yaml'
      break
    case '.xml':
      type = 'file-type-xml'
      break
    case '.html':
      type = 'file-type-html'
      break
    case '.php':
      type = 'file-type-php'
      break
    case '.css':
      type = 'file-type-css'
      break
    case '.js':
      type = 'file-type-js'
      break
    case '.apk':
      type = 'file-type-apk'
      break
    case '.less':
      type = 'file-type-php'
      break
    case '.py':
      type = 'file-type-py'
      break
    case '.sql':
      type = 'file-type-sql'
      break
    case '.json':
      type = 'file-type-json'
      break
    default:
      type = 'file-type-code'
      break
  }
  return type
}

const txtType = fileSuffix => {
  let type
  switch (fileSuffix) {
    case '.md':
      type = 'file-type-md'
      break
    default:
      type = 'file-type-text'
      break
  }
  return type
}

const fileType = fileSuffix => {
  let type
  switch (fileSuffix) {
    case '.exe':
      type = 'file-type-exe'
      break
    default:
      type = 'file-type-file'
      break
  }
  return type
}

// 处理较长文件名，保留文件后缀
const shortName = fileName => {
  const maxLength = 18 // 最大显示长度(不包括后缀)
  const lastDotIndex = fileName.lastIndexOf('.')

  // 如果没有后缀或者文件名本身就很短，直接返回
  if (lastDotIndex === -1 || fileName.length <= maxLength) {
    return fileName
  }

  // 分离文件名和后缀
  const name = fileName.substring(0, lastDotIndex)
  const extension = fileName.substring(lastDotIndex)

  // 如果文件名部分过长，则省略中间部分
  if (name.length > maxLength) {
    return `${name.substring(0, Math.floor(maxLength / 2))}...${name.substring(name.length - Math.floor(maxLength / 2))}${extension}`
  }

  return fileName
}

export { getFileFontElement, shortName }
